// pages/order/evaluate/evaluate.js
var ajax = require('../../../utils/httpUtil.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgSrcs: null,
    starDesc:'超赞',
    imgDescs:null,
    orderId:null,
    commentStar:5,
    commentPic:null,
    shopTitle:null,
    cunNum:0,
    disabled: false,
    disabledSubmit:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let orderId = options.orderId;
    let shopTitle = options.shopTitle;
    let typeUrl = options.typeUrl;//1订单列表跳转过来的   2订单详情页跳转过来的
    let imgSrcs = 
    [
      '../../../img/order/star1.png', 
      '../../../img/order/star1.png', 
      '../../../img/order/star1.png', 
      '../../../img/order/star1.png', 
      '../../../img/order/star1.png'
    ];

    let imgDescs =['../../../img/order/newpicture.png',null,null];
    that.setData({
      imgSrcs: imgSrcs,
      imgDescs: imgDescs,
      orderId: orderId,
      shopTitle: shopTitle,
      commentStar: 5,
      typeUrl: typeUrl
    })
  },
  // 评价输入框监听
  watchConcent:function(e){
    this.setData({
      cunNum: e.detail.cursor
    });
    if (this.data.cunNum === 140){
      this.setData({
        disabled:true
      })
    }
  },
  /**
   * 提交
   */
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var that = this;
    //防止重复使用
    that.setData({
      disabledSubmit: true
    })
    // 获取用户信息
    wx.getStorage({
      key: 'user',
      success: function (res) {
        console.log(res)
        that.setData({
          userId: res.data.id,
          userNickName: res.data.nickName
        })

        let content = e.detail.value.content;
        console.log(content);
        if (content == null || content == "" || content == undefined) {
          wx.showToast({
            title: '请写下您的评价',
            icon: 'none',
            mask: true,
            duration: 2000
          })
          that.setData({
            disabledSubmit: false
          })
          return;
        } else {
          if (content.length<5){
            wx.showToast({
              title: '最少五个字',
              icon: 'none',
              mask: true,
              duration: 2000
            })
            that.setData({
              disabledSubmit: false
            })
            return;
          }
          ajax.reqPost('xorder/addOrderCommentInfo.do',
            {
              content: content,
              commentStar: that.data.commentStar,
              commentPic: that.data.commentPic,
              orderId: that.data.orderId,
              userId: that.data.userId,
              userNickName: that.data.userNickName
            }, function (res) {
              console.log(res);
              if (res.resultCode == 1000) {
                wx.showToast({
                  title: '评价成功',
                  icon: 'success',
                  mask: true,
                  duration: 2000
                })
                let id = res.returnObject
                that.setData({
                  disabledSubmit: false
                })
                //跳转页面
                console.log(that.data.typeUrl);
                if(that.data.typeUrl==1){//订单列表
                  console.log("订单列表");
                  wx.reLaunch({
                    url: '../../order/orderList/orderList'
                  })
                }else{//订单详情
                  console.log("订单详情");
                  wx.navigateTo({
                    url: '../../order/orderDetails/orderDetails?id=' + that.data.orderId
                  })
                }
              }
            });
        }
      }, fail: function () {
        console.log("用户信息获取失败");
        wx.showToast({
          title: "请先登录",
          icon: 'none',
          mask: true,
          duration: 3000
        })
        //去登陆处理
        that.setData({
          disabledSubmit: false
        })
      }  
    })  
  },

  /**
   * 点击五角星
   */
  getStar: function (e) {
    var that = this;
    //获取当前点击的是第几个星
    let index = e.target.dataset.index;

    //图片展示处理
    if (index>=0){//防止没有点击到星星身上，index获取不到问题

      let imgSrcs =
        [
          '../../../img/order/star1.png',
          '../../../img/order/star1.png',
          '../../../img/order/star1.png',
          '../../../img/order/star1.png',
          '../../../img/order/star1.png'
        ];

      for (var i = 0; i < 5; i++) {
        if (i > index) {
          imgSrcs[i] = '../../../img/order/star2.png';
        }
      }

      //星级和对应的文字
      let starDesc = "超赞";
      let commentStar=5;
      if (index == 0) {//1星：非常差
        starDesc = "非常差";
        commentStar = 1;
      } else if (index == 1) {//2星：差
        starDesc = "差";
        commentStar = 2;
      } else if (index == 2) {//3星：一般
        starDesc = "一般";
        commentStar = 3;
      } else if (index == 3) {//4星：满意
        starDesc = "满意";
        commentStar = 4;
      } else if (index == 4) {//5星：超赞
        starDesc = "超赞";
        commentStar = 5;
      }

      that.setData({
        imgSrcs: imgSrcs,
        starDesc: starDesc,
        commentStar: commentStar,
      })

    }
  },

  /**
   * 图片上传
   */
  uploadPics: function (e) {
    var that=this;
    wx.chooseImage({
      count: 3,  //最多可以选择的图片总数  
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        console.log(res);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        //启动上传等待中...  
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 5000
        })

        //循环提交
        var uploadImgCount = 0;
        var imgDescs = [];
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url: ajax.path + 'sys/upload/uploadPics',
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {'imgIndex': i},
            header: {"Content-Type": "multipart/form-data"},
            success: function (res) {
              res = JSON.parse(res.data);
              console.log(res);
              uploadImgCount++;
              //如果是最后一张,则隐藏等待中  
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
              }

              //图片回显
              callBackUpload(res.returnObject, uploadImgCount, that, imgDescs);
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { console.log(res) }
              })
            }
          });
        }
      }
    });
  }

})


//回显图片
function callBackUpload(url, i, that, imgDescs) {
  console.log(url);
  console.log(i);

  imgDescs[i - 1] = url;
  console.log("========"+imgDescs);
  that.setData({
    imgDescs: imgDescs,
    commentPic: imgDescs
  })
}